package cn.exrick.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {

    public static void addWaterMark(InputStream inputStream, File iconFile, String filePath)
            throws IOException {
        // 加载图标文件
        Image iconImg = ImageIO.read(new FileInputStream(iconFile));
        // 获取图片的宽
        int iconWidth = iconImg.getWidth(null);
        // 获取图片的高
        int iconHeight = iconImg.getHeight(null);

        // 文件转化为图片
        Image srcImg = ImageIO.read(inputStream);
        // 获取图片的宽
        int srcImgWidth = srcImg.getWidth(null);
        // 获取图片的高
        int srcImgHeight = srcImg.getHeight(null);

        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

        // 设置水印的坐标
        int x = srcImgWidth - iconWidth;
        int y = srcImgHeight - iconHeight;

        /*float clarity = 0.9f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,clarity));*/
        g.drawImage(iconImg, x, y, null);  // 画出水印
//        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();

        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream(filePath);
        ImageIO.write(bufImg, "jpg", outImgStream);

        outImgStream.flush();
        outImgStream.close();
    }

    public static void addWaterMark(InputStream inputStream, String content, Color color, Font font, String filePath)
            throws IOException {
        // 文件转化为图片
        Image srcImg = ImageIO.read(inputStream);
        // 获取图片的宽
        int srcImgWidth = srcImg.getWidth(null);
        // 获取图片的高
        int srcImgHeight = srcImg.getHeight(null);

        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
        g.setColor(color); // 设置颜色
        g.setFont(font);   // 设置字体

        // 设置水印的坐标
        int x = srcImgWidth - getWatermarkLength(content, g);
        int y = srcImgHeight - getWatermarkLength(content, g);
        g.drawString(content, x, y);  // 画出水印
        g.dispose();

        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream(filePath);
        ImageIO.write(bufImg, "jpg", outImgStream);

        outImgStream.flush();
        outImgStream.close();
    }

    private static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    public static void main(String[] args) throws IOException {
        File file = new File("D:/3.jpeg");
        /*Font font = new Font("微软雅黑", Font.PLAIN, 35);
        String waterMarkContent= "手机联盟商城";
        Color color = new Color(255,255,255,128);
        ImageUtil.addWaterMark(new FileInputStream(file), waterMarkContent, color, font, "D:/4.jpeg");*/
        File iconFile = new File("D:/29pt_@2x.png");
        ImageUtil.addWaterMark(new FileInputStream(file), iconFile, "D:/4.jpeg");
    }

}
